package com.bjc.sdx.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjc.sdx.R;
import com.bjc.sdx.model.ParentObj;

import java.util.ArrayList;

public class CustomerAdapter extends BaseExpandableListAdapter {
    private ArrayList<ParentObj> datas;
    private Context context;

    public CustomerAdapter(ArrayList<ParentObj> datas, Context context) {
        super();
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getChilds().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getChilds().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    // 是否有稳定的id。跟刷新顺序有关
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder pviewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.group_item, parent, false);
            pviewHolder = new ParentViewHolder();
            pviewHolder.pTextView = (TextView) convertView.findViewById(R.id.group_text);
            pviewHolder.pImageView = (ImageView) convertView.findViewById(R.id.group_indicator);
            convertView.setTag(pviewHolder);
        }

        pviewHolder = (ParentViewHolder)convertView.getTag();
        pviewHolder.pTextView.setTypeface(null, Typeface.BOLD);
        pviewHolder.pTextView.setText(datas.get(groupPosition).getpName());

        if (isExpanded) {
            pviewHolder.pImageView.setImageResource(R.drawable.expand_down);
        } else {
            pviewHolder.pImageView.setImageResource(R.drawable.forward_ios);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {
        ChildViewHolder cViewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.expand_child, parent, false);
            cViewHolder = new ChildViewHolder();
            cViewHolder.cTextView = (TextView)convertView.findViewById(R.id.child_item);
            cViewHolder.cImageView = (ImageView) convertView.findViewById(R.id.child_delete);
            convertView.setTag(cViewHolder);
        }

        cViewHolder = (ChildViewHolder)convertView.getTag();
        cViewHolder.cTextView.setText(datas.get(groupPosition).getChilds().get(childPosition).getcName());

        final int gPosition = groupPosition;
        final int cPosition = childPosition;
        cViewHolder.cImageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("你确定要删除吗?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        datas.get(gPosition).getChilds().remove(cPosition);
                        notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class ParentViewHolder {
        TextView pTextView;
        ImageView pImageView;
    }

    static class ChildViewHolder {
        TextView cTextView;
        ImageView cImageView;
    }
}